﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace XWord.WPF.ApplicationFramework
{
    [XmlType]
    public class WordWebEngine
    {
        [XmlAttribute]
        public string Name { get; set; }

        [XmlAttribute]
        public string URL { get; set; }

        [XmlAttribute]
        public int Priority { get; set; }

        public static string CONFIGPATH = AppDomain.CurrentDomain.BaseDirectory + "Config/WebEngineConfig.xml";

        public void Save()
        {
            CreateDefaultWebEngine();
            IXmlSerializer serializer = new DefaultXmlSerializer();
            serializer.Serialize(this, CONFIGPATH);
        }

        public static void CreateDefaultWebEngine()
        {
            List<WordWebEngine> list = new List<WordWebEngine>();
            WordWebEngine webengine = new WordWebEngine();
            list.Add(CreateWebEngine("etymonline", "http://www.etymonline.com/index.php?term={0}"));
            list.Add(CreateWebEngine("wordinfo", "http://wordinfo.info/words/index.php?v=info&a=view_results&s={0}"));
            list.Add(CreateWebEngine("百度", "http://dict.baidu.com/s?wd={0}"));
            list.Add(CreateWebEngine("谷歌","http://www.google.com.hk/dictionary?langpair=en|zh-cn&q={0}"));
            list.Add(CreateWebEngine("爱词霸", "http://www.iciba.com/search?s={0}"));
            list.Add(CreateWebEngine("海词在线", "http://www.dict.cn/search/?q={0}"));
            list.Add(CreateWebEngine("汇学英语单词王", "http://dict.huixue.cc/s?{0}"));
            IXmlSerializer serializer = new DefaultXmlSerializer();
            serializer.Serialize(list, CONFIGPATH);
        }

        public static List<WordWebEngine> GetWebEngineList()
        {
            IXmlSerializer serializer = new DefaultXmlSerializer();
            return serializer.Deserialize<List<WordWebEngine>>(CONFIGPATH);
        }

        private static WordWebEngine CreateWebEngine(string name, string url)
        {
            WordWebEngine webengine = new WordWebEngine();
            webengine.Name = name;
            webengine.URL = url;
            
            return webengine;
        }

        //public void GoToWebUrl()
        //{

        //}
    }
}
